package com.situ.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class ImageUtil {

	public static BufferedImage image(String code) {
		int width = 250, height = 100;
		//rgb不透明色
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = bi.getGraphics();
		
		// 设置背景
		g.setColor(new Color(230, 230, 230));
		g.fillRect(0, 0, width, height);

		Random rnd = new Random();
		// 画干扰线
		g.setColor(new Color(47, 42, 42));
		/*g.drawLine(0, 0, 250, 100);
		g.drawLine(140, 100, 250, 0);
		g.drawLine(30, 20, 250, 18);
		g.drawLine(50, 222, 78, 100);
		g.drawLine(199, 100, 478, 118);
		g.drawLine(228, 318, 258, 538);
		g.drawLine(7, 0, 25, 250);
		g.drawLine(250, 100, 111, 230);
		g.drawLine(200, 78, 111, 145);*/
		for(int i =0;i<100;i++) {
			int x =rnd.nextInt(width);
			int y = rnd.nextInt(height);
			int x1 = rnd.nextInt(2);
			int y1 = rnd.nextInt(4);
			g.drawOval(x,y,x+x1,y+y1);
		}

		// 设置颜色，字体，写出内容
		g.setColor(new Color(30, 30, 30));
		g.setFont(new Font("Times New Roman", Font.PLAIN, 80));
		g.drawString(code, 10, 80);

		// 生成图像
		g.dispose();
		return bi;
	}

}
